from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.db.database import get_db
from app.models.user import User
from app.schemas.user_schema import UserOut, ApiResponse

router = APIRouter()


@router.get("/user", response_model=ApiResponse)
def get_users(db: Session = Depends(get_db)):
    users = db.query(User).all()
    if users:
        user_list = [UserOut.from_orm(user) for user in users]
        return {"code": "200", "msg": "获取成功", "data": user_list}
    else:
        return {"code": "200", "msg": "获取成功", "data": ""}


@router.get("/user/{user_id}", response_model=ApiResponse)
def get_user(user_id: int, db: Session = Depends(get_db)):
    user = db.query(User).filter(User.id == user_id).first()
    if not user:
        raise HTTPException(status_code=404, detail="User not found")

    user_out = UserOut.from_orm(user)
    return {"code": "200", "msg": "获取成功", "data": user_out}